package com.ruicar.afs.cloud.apply.business.mq.synchro;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleModel;
import com.ruicar.afs.cloud.parameter.commom.entity.VeBrand;
import com.ruicar.afs.cloud.parameter.commom.service.TsysVehicleModelService;
import com.ruicar.afs.cloud.parameter.commom.vo.VeBrandVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Ice
 * @date 2020/9/16 13:59
 */
@Slf4j
@Component
@AllArgsConstructor
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ApplyChannelSynchroBrand implements AfsMqBizProcessor<VeBrandVo> {
    private final TsysVehicleModelService tsysVehicleModelService;
    @Override
    public boolean processMessage(VeBrandVo entity) throws Exception {

        if (ObjectUtil.isNotNull(entity)) {
            log.info("已成功接收到车型库MQ报文，解析报文中... ......(*￣０0000￣)ノ");
            this.getFileterParams(entity.getVeBrandList());
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.CHANNEL_CHANNEL_APPLY_POS_SEND_VEHICLE_BRAND_APPLY;
    }

    @Override
    public boolean reQueueOnFail() {
        return false;
    }


    /**
     * 解析MQ报文
     *
     * @param entity
     */
    public void getFileterParams(List<VeBrand> entity) throws ParseException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(entity.size()>0){
            for (VeBrand brand:entity){
                TsysVehicleModel model = tsysVehicleModelService.getOne(Wrappers.<TsysVehicleModel>query().lambda()
                        .eq(TsysVehicleModel::getCode,brand.getCode()));
                if(model==null){
                    // 说明没有 我要去添加
                    TsysVehicleModel m=new TsysVehicleModel();
                    m.setCode(brand.getCode());
                    m.setModels(brand.getBrand());
                    m.setBrandCode(brand.getMaker());
                    if(brand.getOrderid()!=null&&brand.getOrderid()!=""){
                        m.setOrderId(Integer.valueOf(brand.getOrderid()));
                    }
                    m.setIsActive(brand.getIsactive());
                    m.setStyleRule(brand.getStylerule());
                    m.setFullName(brand.getFullname());
                    m.setCmsId(brand.getCmsid());
                    m.setType(brand.getType());
                    m.setSeriesId(brand.getSeriesid());
                    m.setAdditionalTag(brand.getAdditionaltag());
                    m.setNeedSync(brand.getNeedsync());
                    m.setCreateBy(brand.getCreateuser());
                    m.setUpdateTime(fmt.parse(brand.getUpdatedate()));
                    m.setUpdateBy(brand.getUpdateuser());
                    m.setDelFlag(brand.getIsdelete());
                    m.setCreateTime(new Date(System.currentTimeMillis()));
                    m.setUpdateTime(new Date(System.currentTimeMillis()) );
                    tsysVehicleModelService.save(m);
                }else {
                    // 说明存在去更新
                    model.setCode(brand.getCode());
                    model.setModels(brand.getModels());
                    model.setBrandCode(brand.getMaker());
                    if(brand.getOrderid()!=null&&brand.getOrderid()!=""){
                        model.setOrderId(Integer.valueOf(brand.getOrderid()));
                    }
                    model.setIsActive(brand.getIsactive());
                    model.setStyleRule(brand.getStylerule());
                    model.setFullName(brand.getFullname());
                    model.setCmsId(brand.getCmsid());
                    model.setType(brand.getType());
                    model.setSeriesId(brand.getSeriesid());
                    model.setAdditionalTag(brand.getAdditionaltag());
                    model.setNeedSync(brand.getNeedsync());
                    model.setCreateBy(brand.getCreateuser());
                    model.setUpdateBy(brand.getUpdateuser());
                    model.setDelFlag(brand.getIsdelete());
                    model.setCreateTime(new Date(System.currentTimeMillis()));
                    model.setUpdateTime(new Date(System.currentTimeMillis()) );
                    tsysVehicleModelService.updateById(model);
                }
            }
        }

    }
}
